home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000056_icon-group-sender _Tue Mar 23 09:20:56 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  2KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id JAA28013
  4.     for icon-group-addresses; Tue, 23 Mar 1999 09:20:49 -0700 (MST)
  5. Message-Id: <199903231620.JAA28013@baskerville.CS.Arizona.EDU>
  6. To: icon-group@optima.CS.Arizona.EDU
  7. Date: Tue, 23 Mar 1999 08:54:14 -0500
  8. From: Robert Wyesham <rwyesham@earthlink.net>
  9. Subject: Help for the Newbie!
  10. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  11. Status: RO
  12.  
  13. I'd like to define an "assert" macro for use in my Icon programs, using ipp, of
  14. course.
  15. I need criticism and advice regarding the definition that I'm proposing.
  16.  
  17. I decided to steal from the best, so I looked up the definition that P.J.
  18. Plauger used in his book, "The Standard C Library." Here it is, with
  19. line-numbering added, in order to make it easy to refer to (any mistakes are my
  20. own):
  21.  
  22. 1. #undef assert
  23. 2. #ifdef NDEBUG
  24. 3. #define assert(test) ((void)0)
  25. 4. #else
  26. 5. #define _STR(x) _VAL(x)
  27. 6. #define _VAL(x) #x
  28. 7. #define assert(test) (test) ? (void)0 : \
  29. 8.                                _Assert( __FILE__ ":" _STR(__LINE__) #test )
  30. 9. #endif
  31.  
  32. The _STR and _VAL macros turn the __LINE__ macro from an integer into a string.
  33. Dr. Plauger defined assert as a ternary expression, rather than as an 'if'
  34. statement, so that crazy people could use the comma operator to do deranged
  35. things like:
  36.  
  37.                                 ( assert(0 < x), x < y )
  38.  
  39. Without an assert macro, I've been writing:
  40.  
  41. $ifndef NDEBUG
  42. if test then
  43.     (&null)
  44. else
  45.     stop( "--- assertion failed: file ", &file, ", line ", &line )
  46. $endif
  47.  
  48. I'm proposing this:
  49.  
  50. 10. $undef assert
  51. 11. $ifdef NDEBUG
  52. 12. $define assert(test) (&null)
  53. 13. $else
  54. 14. $define assert(test) if test then (&null) else \
  55. 15.                                 stop( "--- assertion failed: file ", &file,
  56. ", line ", &line )
  57. 16. $endif
  58.  
  59. Does anyone see anything basically wrong with this definition? Are there
  60. mistakes in it?
  61. Are there better ways to do this? A big problem with it is that the test never
  62. gets printed.
  63. I have no idea how to turn the test into a string that I can print. ipp and Icon
  64. must have a way to do this, but I haven't thought of it. Of course, in a macro,
  65. everything's a string. At this point, I get confused. Any ideas?
  66.  
  67. Thanks and best regards,
  68. - rob
  69.  
  70. Rob Wyesham,
  71. Wilmington, Delaware
  72.  
  73.  
  74.  
  75.